package cn.edu.cdut.service;

import cn.edu.cdut.dao.PlanCourseInfoDao;
import cn.edu.cdut.dao.StudentInfoDao;
import cn.edu.cdut.dao.WishInfoDao;
import cn.edu.cdut.dto.MyRes;
import cn.edu.cdut.dto.WishCount;
import cn.edu.cdut.model.PlanCourseInfo;
import cn.edu.cdut.model.StudentInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.stylesheets.LinkStyle;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Author  kobe
 * Created on 2017-06-21-0:31
 */
@Service
public class WishCountService {

        @Resource
        private StudentInfoDao studentInfoDao;

        @Autowired
        private PlanCourseInfoDao planCourseInfoDao;

        @Autowired
        private WishInfoDao wishInfoDao;

        //获得统计
        @Transactional
        public MyRes getWishCountData() {
                //gradeID
                String gradeID = null;
                Iterable<PlanCourseInfo> planCourseInfos = planCourseInfoDao.findAll();
                Iterator<PlanCourseInfo> iterator = planCourseInfos.iterator();
                if (iterator.hasNext()) {
                        PlanCourseInfo planCourseInfo = iterator.next();
                        gradeID = planCourseInfo.getGrade();
                }
                int stuNum = studentInfoDao.findByGrade(gradeID).size();
                int wishNum = wishInfoDao.findByGrade(gradeID).size();
                //统计
                WishCount wishCount = new WishCount();
                wishCount.setGrade(gradeID);
                wishCount.setStuTotal(stuNum);
                wishCount.setWishTotal(wishNum);
                wishCount.setUnwishTotal(stuNum-wishNum);

                //转换
                ObjectMapper mapper = new ObjectMapper();
                String res;
                try {
                        res = mapper.writeValueAsString(wishCount);
                } catch (JsonProcessingException e) {
                        e.printStackTrace();
                        return new MyRes(201, "系统异常", null);
                }
                return new MyRes(200, null, res);
        }

        //获得未填报名单
        @Transactional
        public MyRes getUnwishData() {
                String gradeID = null;
                Iterable<PlanCourseInfo> planCourseInfos = planCourseInfoDao.findAll();
                Iterator<PlanCourseInfo> iterator = planCourseInfos.iterator();
                if (iterator.hasNext()) {
                        PlanCourseInfo planCourseInfo = iterator.next();
                        gradeID = planCourseInfo.getGrade();
                }

                List<StudentInfo> studentInfos = studentInfoDao.findByGrade(gradeID);
                List<StudentInfo> unwishList = new ArrayList<>();
                int i = 0;
                for (StudentInfo studentInfo : studentInfos) {
                        if (wishInfoDao.findByStuId(studentInfo.getStuId()) == null) {
                                unwishList.add(i,studentInfo);
                                i++;
                        }
                }
                ObjectMapper mapper = new ObjectMapper();
                String[] resArr = new String[unwishList.size()];
                int j = 0;
                for (StudentInfo studentInfo : unwishList) {
                        try {
                                resArr[j] = mapper.writeValueAsString(studentInfo);
                                j++;
                        } catch (JsonProcessingException e) {
                                e.printStackTrace();
                                return new MyRes(201, "系统异常！", null);
                        }
                }
                return new MyRes(200, null, resArr);
        }
}
